import Modal from './Modal.vue';
import { createApp } from 'vue';
import type { IAlert, IConfirm } from './interface';

export function Alert(opt: IAlert): Promise<boolean> {
    return new Promise(resolve => {
        const alert = document.createElement('div');
        const app = createApp(Modal, {
            show: true,
            title: opt.title,
            content: opt.message,
            width: 240,
            height: 200,
            showCancel: false,
            showConfirm: true,
            beforeConfirm: () => {
                resolve(true);
                app.unmount();
                alert.remove();
            },
        });
        app.mount(alert);
        document.body.appendChild(alert);
    });
}

export function Confirm(opt: IConfirm): Promise<boolean> {
    return new Promise(resolve => {
        const confirm = document.createElement('div');
        const app = createApp(Modal, {
            show: true,
            title: opt.title,
            content: opt.message,
            width: 240,
            height: 200,
            showCancel: true,
            showConfirm: true,
            beforeCancel: () => {
                opt.onCancel && opt.onCancel();
                resolve(false);
                app.unmount();
                confirm.remove();
            },
            beforeConfirm: () => {
                opt.onConfirm && opt.onConfirm();
                resolve(true);
                app.unmount();
                confirm.remove();
            },
        });
        app.mount(confirm);
        document.body.appendChild(confirm);
    });
}
